<template>

  <!-- 搜索框 -->
  <el-card style="height: 80px">
    <el-form :model="queryForm" :inline="true" class="form">
      <el-form-item label="名称:">
        <el-input
            v-model="queryForm.queryCategoryName"
            placeholder="请输入名称"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" size="default" @click="search">
          搜索
        </el-button>
      </el-form-item>
    </el-form>
  </el-card>


  <!-- 添加用户和批量删除 -->
  <el-card style="margin: 10px 0">
    <el-tooltip effect="dark" content="添加" placement="top">
      <el-button
          type="success"
          size="default"
          icon="Plus"
          @click="handleAdd"
      >
      </el-button>
    </el-tooltip>
    <!-- table展示用户信息 -->
    <el-table
        style="margin: 10px 0"
        border
        :data="typeTableData"
    >
      <el-table-column
          label="名称"
          align="center"
          prop="categoryName"
          show-overflow-tooltip
      ></el-table-column>
      <el-table-column
          label="描述"
          align="center"
          prop="description"
          show-overflow-tooltip
      ></el-table-column>
      <el-table-column label="操作" fixed="right" width="120px" align="center">
        <template #="{ row }">
          <el-tooltip effect="dark" content="编辑" placement="top">
            <el-button
                type="primary"
                size="small"
                icon="Edit"
                @click="handleEdit(row)"
            >
            </el-button>
          </el-tooltip>
          <el-tooltip effect="dark" content="删除" placement="top">
            <el-button
                type="danger"
                size="small"
                icon="Delete"
                @click="handleDelete(row.categoryId)"
            >
            </el-button>
          </el-tooltip>
        </template>

      </el-table-column>
    </el-table>
    <!-- 分页器 -->
    <el-pagination
        v-model:current-page="pageNo"
        v-model:page-size="pageSize"
        :page-sizes="[5, 10, 15, 20]"
        :background="true"
        layout="prev, pager, next, jumper,->,sizes,total"
        :total="total"
        @current-change="findAll"
        @size-change="handler"
    />
  </el-card>

  <!-- 抽屉结构:完成添加新的用户账号|更新已有的账号信息 -->
  <el-drawer v-model="drawer" size="1000px">
    <!-- 头部标题:将来文字内容应该动态的 -->
    <template #header>
      <h4>{{ typeForm.categoryId ? '更新' : '添加' }}</h4>
    </template>
    <!-- 身体部分 -->
    <template #default>
      <el-form :model="typeForm" :rules="rules" ref="formRef">
        <el-form-item label="名称" prop="categoryName">
          <el-input
              placeholder="请您输入名称"
              v-model="typeForm.categoryName"
          ></el-input>
        </el-form-item>
        <el-form-item label="描述" prop="description">
          <el-input
              type="textarea"
              placeholder="请您输入描述"
              v-model="typeForm.description"
          ></el-input>
        </el-form-item>
      </el-form>
    </template>
    <template #footer>
      <div style="flex: auto">
        <el-button @click="drawer = false">取消</el-button>
        <el-button type="primary" @click="save">确定</el-button>
      </div>
    </template>
  </el-drawer>


</template>

<script setup lang="ts">
import {ref, reactive, onMounted, nextTick} from 'vue'
import {ElFormItem, ElMessageBox} from 'element-plus'
import {apiGet, apiPost} from "@/utils/api.ts";
// 表单数据
let typeForm = reactive(
    {
      categoryId: 0,
      categoryName: '',
      description: '',
    })
//搜索表单数据
let queryForm = reactive({
  queryCategoryName: '',
})
//默认页码
let pageNo = ref<number>(1)
//一页展示几条数据
let pageSize = ref<number>(5)
//总个数
let total = ref<number>(0)
//存储全部的数组
let typeTableData = ref([])
//定义响应式数据控制抽屉的显示与隐藏 添加用户和修改用户
let drawer = ref<boolean>(false)
//获取form组件实例
let formRef = ref<any>()
//获取全部用户信息
let findAll = async (pager = 1) => {
  //收集当前页码
  pageNo.value = pager
  let parameter = {
    page: pageNo.value,
    limit: pageSize.value,
    categoryName: queryForm.queryCategoryName
  }
  //获取表单数据
  let {data} = await apiGet('/courseCategories/findAll', parameter)
  total.value = data.count
  typeTableData.value = data.data
}

//分页器下拉菜单的自定义事件的回调
const handler = () => {
  findAll()
}

//搜索按钮的回调
const search = () => {
  findAll()
}

//添加用户按钮的回调
const handleAdd = () => {
  //抽屉显示出来
  drawer.value = true
  //清空userParams的数据以及userParams继承User里面的所有数据
  Object.assign(typeForm, {
    categoryId: 0,
    categoryName: '',
    description: '',
  })
  //清除上一次的错误的提示信息
  nextTick(() => {
    formRef.value.clearValidate()
  })
}

//更新用户按钮的回调
const handleEdit = (row: any) => {
  //抽屉显示出来
  drawer.value = true
  //将用户的信息赋值给userParams
  Object.assign(typeForm, {
    ...row
  })
  //清除上一次的错误的提示信息
  nextTick(() => {
    formRef.value.clearValidate()
  })
}

//保存按钮的回调
const save = async () => {
  //点击保存按钮的时候,务必需要保证表单全部复合条件在去发请求
  await formRef.value.validate()
  //保存按钮:添加新的用户|更新已有的用户账号信息
  typeForm.categoryId ? await apiPost('/courseCategories/edit', {...typeForm}) : await apiPost('/courseCategories/add', {...typeForm})
  await findAll()
  drawer.value = false
}

//删除
const handleDelete = async (categoryId: number) => {
  //提示用户确认删除吗
  ElMessageBox.confirm('此操作将永久删除该分类, 是否继续?', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning',
  })
      .then(async () => {
        await apiPost('/courseCategories/delete', {categoryId})
        await findAll(typeTableData.value.length > 1 ? pageNo.value : pageNo.value - 1)
      })
      .catch(() => {
      })
}


const validatorName = (rule: any, value: string, callback: any) => {
  if (value === '') {
    callback(new Error('请输入用户名'));
  } else {
    callback();
  }
}

//表单校验的规则对象
const rules = {
  categoryName: [{required: true, trigger: 'blur', validator: validatorName}],
}

//组件挂载完毕
onMounted(() => {
  findAll()
})
</script>

<style scoped>
</style>
